%{
This function sets up solving and equilibrium on a grid of parameters
%}

tic;
clc;
clear all;
close all;

% Set up an even grid
%{
lb = [8, 1.3, 10000, 0.1];
ub = [10, 1.8, 50000, 0.4];
gridpoints = 2; 

mu_fc_grid       = lb(1): (ub(1)-lb(1))/gridpoints: lb(1) + (gridpoints-1)*(ub(1)-lb(1))/gridpoints; % mean fixed cost of remaining in the market
sigma_fc_grid    = lb(2): (ub(2)-lb(2))/gridpoints: lb(2) + (gridpoints-1)*(ub(2)-lb(2))/gridpoints; % sd of fixed cost of remaining in the market
c_e_grid         = lb(3): (ub(3)-lb(3))/gridpoints: lb(3) + (gridpoints-1)*(ub(3)-lb(3))/gridpoints; % entry cost
delt_grid        = lb(4): (ub(4)-lb(4))/gridpoints: lb(4) + (gridpoints-1)*(ub(4)-lb(4))/gridpoints; % fraction that go to a random agent

State = combvec(1:1:gridpoints, 1:1:gridpoints, 1:1:gridpoints, 1:1:gridpoints);
State(5,:) = 1:1:gridpoints^4; %% corresponding to the iteration
%}

%Alternatively specify directly:
mu_fc_grid       = 7:0.1:10; % mean fixed cost of remaining in the market
sigma_fc_grid    = 1:0.1:2; % sd of fixed cost of remaining in the market
c_e_grid         = 10000:1000:30000; % entry cost
delt_grid        = .1:.1:.8; % fraction that go to a random agent
State = combvec(1:1:size(mu_fc_grid,2), 1:1:size(sigma_fc_grid,2), 1:1:size(c_e_grid,2), 1:1:size(delt_grid,2));
size(State,2)


State(5,:) = 1:1:size(State,2); %% corresponding to the iteration
size(State,2)


%parfor iter = 1:size(State,2)
for iter = 1:size(State,2)
    p1 = mu_fc_grid(State(1,iter));
    p2 = sigma_fc_grid(State(2,iter));
    p3 = c_e_grid(State(3,iter));
    p4 = delt_grid(State(4,iter));
    saveas = ['Baseline',num2str(iter)];
    fun([p1 p2 p3 p4],saveas);
end

toc;
